package com.xsmart.oa.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.QueryDistributorDAO;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分销商管理界面
 * @author lxh
 * @date 2017/11/23
 */
@Service
public class QueryDistributorService {
    @Autowired
    QueryDistributorDAO queryDistributorDAO;

    /**
     * 查询分销商
     * @param requestBean
     * @return
     */
    public ResponseBean queryDistributor(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        String taskType = requestBean.getRequestParamString("taskType");
        String status = requestBean.getRequestParamString("status");
        int currentPage=requestBean.getRequestParamInteger("currentPage",1);
        int pageSize=requestBean.getRequestParamInteger("pageSize",10);
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        //通过taskType查询对应分销商和供应商的记录
        page.setRecords(queryDistributorDAO.queryAllDistributor(page,taskType, status));
        //各个申请记录的条数
        responseBean.addResultItem("amount",queryDistributorDAO.queryDistributorAmount(taskType));
        responseBean.addResultPageItem("allDistributor",page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 查询审核通过的记录和未通过的记录
     * @param requestBean
     * @return
     */
    public ResponseBean queryPassAndUnPass(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        int currentPage=requestBean.getRequestParamInteger("currentPage",1);
        int pageSize=requestBean.getRequestParamInteger("pageSize",10);
        String taskType = requestBean.getRequestParamString("taskType");
        String status = requestBean.getRequestParamString("status");
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        //查询通过和未通过的记录
        page.setRecords(queryDistributorDAO.queryPassAndUnPasssDistributor(page,taskType, status));
        responseBean.addResultPageItem("passAndUnPass",page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 修改申请成为供应商的审核状态
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean updateDistributor(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer taskId = requestBean.getRequestParamInteger("taskId");
        Integer status = requestBean.getRequestParamInteger("status");
        Integer adminId = requestBean.getRequestParamInteger("adminId");
        Integer userId = requestBean.getRequestParamInteger("userId");
        String checkRemark = requestBean.getRequestParamString("checkRemark");
        String examine = requestBean.getRequestParamString("examine");
        try{
             queryDistributorDAO.updateDistributor(status, adminId, checkRemark, taskId);
        }catch(Exception e){
            responseBean.setResult(ResultConstant.ERROR);
            e.printStackTrace();
            throw new RuntimeException();
        }
        if (status == 1 && examine.equals("distributor")) {
            //更新分销商
            try{
                queryDistributorDAO.updateInfoUser(userId);
                responseBean.setResult(ResultConstant.SUCCESS);
            }catch(Exception e){
                responseBean.setResult(ResultConstant.ERROR);
                throw new RuntimeException();
            }
        } else if (status == 1 && examine.equals("supplier")) {
            //更新供应商
            Map<String, String> InfsUserMap = queryDistributorDAO.queryInfsUserById(userId);
            if (InfsUserMap != null) {
                String userName = InfsUserMap.get("user_name");
                String email = InfsUserMap.get("email");
                String password = InfsUserMap.get("password");
                Integer roleId = 1;
                if (email == null) {
                    email = "";
                }
                try {
                    //通过的申请将会员插入到管理员表
                 queryDistributorDAO.insertInfsAdminUser(userId,userName, email, password, roleId);
                 queryDistributorDAO.updateSellerShopinfo(status,userId);
                 responseBean.setResult(ResultConstant.SUCCESS);
                } catch (Exception e) {
                    responseBean.setResult(ResultConstant.ERROR);
                    e.printStackTrace();
                    throw new  RuntimeException();
                }
            }
        } else if(status==2 && examine.equals("supplier")){
            try {
                //不通过审核将店铺状态改为未通过
                queryDistributorDAO.updateSellerShopinfo(status,userId);
                responseBean.setResult(ResultConstant.SUCCESS);
            } catch (Exception e) {
                responseBean.setResult(ResultConstant.ERROR);
                e.printStackTrace();
                throw new  RuntimeException();
            }
        }
        return responseBean;
    }

    /**
     * 修改申请成为分销商的审核状态
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean newUpdateDistributor(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer taskId = requestBean.getRequestParamInteger("taskId");
        Integer status = requestBean.getRequestParamInteger("status");
        Integer adminId = requestBean.getRequestParamInteger("adminId");
        Integer userId = requestBean.getRequestParamInteger("userId");
        String checkRemark = requestBean.getRequestParamString("checkRemark");
        String examine = requestBean.getRequestParamString("examine");
        String userName=requestBean.getRequestParamString("user_name");
        String phone=requestBean.getRequestParamString("phone");
        String sellerName=requestBean.getRequestParamString("seller_name");
        Map<String,Object> sellerShopInfoMap = new HashMap<>();
        sellerShopInfoMap.put("id",null);
        //默认数据
        Integer shopType=1;
        String shopTitle="标题";
        String shopKeyword="关键字";
        Integer country=1;
        Integer province=1;
        Integer city=1;
        Integer distrct=1;
        String shopAddress="地址";
        String  mobile="12345678911";
        Integer shopStatus=1;
        Integer auditStatus=1;
        //更新infu_user_request_type_task
        try{
            int flag=queryDistributorDAO.newUpdateDistributor(status,checkRemark,userName,phone,sellerName,taskId);
        }catch(Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }

        if (status==1){
            Map<String,Object> shippingTempletMap = new HashMap<>();
            shippingTempletMap.put("shippingTempletName","测试模板");
            shippingTempletMap.put("shippingTempletRemark","测试");
            shippingTempletMap.put("shippingTempletStatus",1);
            shippingTempletMap.put("shippingTitle","测试");
            shippingTempletMap.put("templetType",1);
            try{
                //更新info_user表
                int changeNumber=queryDistributorDAO.updateInfoUser(userId);
                int num=queryDistributorDAO.insertInfoDistributor(userId,shopType,sellerName,shopTitle,shopKeyword,country,province,city,distrct,shopAddress,mobile,shopStatus,auditStatus,sellerShopInfoMap);
                shippingTempletMap.put("sellerShopinfoId",MapUtils.getString(sellerShopInfoMap,"id"));
                num =queryDistributorDAO.insertShippingTemplet(shippingTempletMap);
             }catch(Exception e){
                e.printStackTrace();
                responseBean.setResult(ResultConstant.ERROR);
                throw new RuntimeException();
            }

        }
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }
}
